package game.Russian;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GameGui extends JFrame {

	private static final long serialVersionUID = 1L;
	//左侧游戏面板
	private JPanel left=new JPanel(null);
	private JLabel next=new JLabel("下一个图形");
	//右侧信息面板
	private JPanel right=new JPanel(null);
	private JButton start=new JButton("开始游戏");
	//当前图形
	private TetrisBlock currGrap=null;
	//下一个图形
	private TetrisBlock nextGrap=null;
	//左侧背景游戏图形
	private TetrisBase te=null;
	private KeyListener gameLi=getPlayListener();

	public GameGui() {
		super("俄罗斯方块");
		this.setBounds(400, 200, 750, 750);
		this.setVisible(true);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void initGUI() {
		this.setLayout(null);
		left.setBounds(10, 20, 500, 700);
		left.setBorder(BorderFactory.createLineBorder(Color.RED));
		this.add(left);
		next.setBounds(580,50,150,30);
		next.setFont(new Font("宋体", Font.BOLD, 20));
		this.add(next);
		start.setBounds(575, 280, 120, 30);
		this.add(start);
		start.addActionListener((e)->{

			this.setFocusable(true);
			start.setEnabled(false);
			startGame();
		});

		right.setBounds(580,100,120,120);
		this.add(right);

		drawRight();
		drawLeft(te);
	}

	//开始游戏
	public void startGame() {
		te=new TetrisBase();
		currGrap=TetrisBlock.getRandomBlock();
		nextGrap=TetrisBlock.getRandomBlock();
		//绑定监听
		this.addKeyListener(gameLi);
		Option.rest(currGrap);
		Matrix temp=Option.moveDown(te, currGrap,0);
		drawLeft(temp);
		drawRight();
		auto();
	}

	public void auto() {
		Runnable r=()->{

			while(this.getKeyListeners().length>0) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Matrix temp=Option.moveDown(te,currGrap,1);
				if(Option.isOption()){
					te.setValue(temp);
					nextOption();
					drawLeft(te);
				}else {
					drawLeft(temp);
				}
				te.clearRow();
			}
		};

		new Thread(r).start();
	}

	public void nextOption() {
		currGrap=nextGrap;
		nextGrap=TetrisBlock.getRandomBlock();
		Option.rest(currGrap);
		drawRight();
		Matrix temp=Option.moveDown(te, currGrap,0);
		if(Option.isCoincide(temp)) {
			JOptionPane.showMessageDialog(this, "游戏结束");
			this.removeKeyListener(gameLi);
			start.setEnabled(true);
		}

	}


	public KeyListener getPlayListener() {
		KeyListener listener=new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {}

			@Override
			public void keyReleased(KeyEvent e) {}

			/**
			 * e.getKeyCode():
			 * 37左移动
			 * 39右移动
			 * 38旋转（右）
			 * 40向下加速
			 */
			@Override
			public void keyPressed(KeyEvent e) {
				int code=e.getKeyCode();
				Matrix temp=te;
				switch (code) {
					case 37:
						temp=Option.yMove(te,currGrap,-1);
						break;
					case 38:
						temp=Option.flip(te, currGrap);
						break;
					case 39:
						temp=Option.yMove(te,currGrap,1);
						break;
					case 40:
						temp=Option.moveDown(te,currGrap,1);
						break;
					default:
						return;
				}

				if(Option.isOption()){
					te.setValue(temp);
					nextOption();
					drawLeft(te);
				}else {
					drawLeft(temp);
				}
				te.clearRow();
			}
		};

		return listener;
	}

	public void drawLeft(Matrix temp) {
		if(temp==null) return;

		int baseSize=50;
		int[][] array=temp.get();
		left.removeAll();
		for(int i=0;i<TetrisBase.row;i++) {
			for(int j=0;j<TetrisBase.column;j++) {
				if(array[i][j]==1) {
					JButton bt=new JButton();
					bt.setBounds(j*baseSize,i*baseSize,baseSize,baseSize);
					bt.setBackground(Color.DARK_GRAY);
					bt.setEnabled(false);
					left.add(bt);
				}
			}
		}
		left.repaint();
	}

	public void drawRight() {
		if(nextGrap==null) return;
		int[][] array=nextGrap.get();
		int baseSize=30;

		right.removeAll();
		for(int i=0;i<nextGrap.getRow();i++) {
			for(int j=0;j<nextGrap.getColumn();j++) {
				if(array[i][j]==1) {
					JButton bt=new JButton();
					bt.setBounds(j*baseSize, i*baseSize,baseSize,baseSize);
					bt.setBackground(nextGrap.getColor());
					bt.setEnabled(false);
					right.add(bt);
				}
			}
		}
		right.repaint();
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(()->{
			new GameGui().initGUI();
		});
	}
}
